# GET Command Generator
# http://code.google.com/p/pysnmpcollector/
from pysnmp.entity.rfc3413.oneliner import cmdgen
from pysnmp.proto import rfc1902


def setsnmp(ndip,oid,type,value):

    if type=="OctetString":
        errorIndication, errorStatus, errorIndex, varBinds = cmdgen.CommandGenerator().setCmd(cmdgen.CommunityData(ndip, 'private'),cmdgen.UdpTransportTarget((ndip, 161)),(oid, rfc1902.OctetString(value)))
    if type=="IpAddress":
        errorIndication, errorStatus, errorIndex, varBinds = cmdgen.CommandGenerator().setCmd(cmdgen.CommunityData(ndip, 'private'),cmdgen.UdpTransportTarget((ndip, 161)),(oid, rfc1902.IpAddress(value)))
    if type=="Integer":
        errorIndication, errorStatus, errorIndex, varBinds = cmdgen.CommandGenerator().setCmd(cmdgen.CommunityData(ndip, 'private'),cmdgen.UdpTransportTarget((ndip, 161)),(oid, rfc1902.Integer(value)))
        
    if errorIndication:
        print errorIndication
    else:
        if errorStatus:
            print '%s at %s\n' % (
                errorStatus.prettyPrint(),
                errorIndex and varBinds[int(errorIndex)-1] or '?'
                )
        else:
            for name, val in varBinds:
                print '%s = %s' % (name.prettyPrint(), val.prettyPrint())
                            
def getsnmp(ndip,oid):
    errorIndication, errorStatus, errorIndex, varBinds = cmdgen.CommandGenerator().getCmd(cmdgen.CommunityData(ndip, 'public'),cmdgen.UdpTransportTarget((ndip, 161)),oid,)

    if errorIndication:
        print errorIndication
        # conn.execute("update rnd set snmp"+str(oid[-1])+"='"+errorIndication+"' where ndip='"+ndip+"'")
    else:
        if errorStatus:
            print '%s at %s\n' % (
                errorStatus.prettyPrint(),
                errorIndex and varBinds[int(errorIndex)-1] or '?'
                )
            # conn.execute("update rnd set snmp"+str(oid[-1])+"='"+rrorStatus.prettyPrint()+"' where ndip='"+ndip+"'")
        else:
                # print varBinds
            for name, val in varBinds:
                # print ndip
                print '%s:%s = %s' % (ndip,name.prettyPrint(), val.prettyPrint())
                # print '-'.join('%02x' % ord(c) for c in  val)
                # print oid[-1]
                # conn.execute("update rnd set snmp"+str(oid[-1])+"='"+val.prettyPrint()+"' where ndip='"+ndip+"'")
getsnmp('172.17.104.242',(1,3,6,1,4,1,5959,1,1,2,1,1,6,1,2))                
    
                    